<?php

Route::get('language/{lang}', ['as' => 'set-lang', function($lang) {
    if (in_array($lang, Config::get('app.language'))) {
        $user           = User::find(Auth::user()->id);
        $user->language = $lang;
        $user->save();
    }
    return Redirect::route('home');
}]);

Route::group(['before' => 'guest'], function() {
    Route::any('log-in', [
        'as'   => 'login',
        'uses' => 'HomeController@login'
    ]);
});

Route::group(['before' => 'auth'], function() {

    if (Auth::check()) {
        if (in_array(Auth::user()->language, Config::get('app.language'))) {
            App::setLocale(Auth::user()->language);
        } else {
            Auth::user()->language = Config::get('app.locale');
        }
    }

    Route::get(null, [
        'as'   => 'home',
        'uses' => 'HomeController@index'
    ]);

    Route::group(array('prefix' => 'statistic'), function() {
        Route::post('/attendance/{scoretype}', [
            'as'   => 'home-statistic-attendance',
            'uses' => 'HomeController@postAjaxAttendance'
        ]);
        Route::post('/review/{type}', [
            'as'   => 'home-statistic-review',
            'uses' => 'HomeController@postAjaxReview'
        ]);
    });

    Route::post('/searchbox', [
        'as'   => 'home-searchbox',
        'uses' => 'HomeController@postAjaxSearchBox'
    ]);

    Route::get('error', [
        'as'   => 'error-pages',
        'uses' => 'ErrorController@index'
    ]);

    Route::get(CNamHoa::slugNH('system.logout'), [
        'as'   => 'logout',
        'uses' => 'HomeController@logout'
    ]);

    Route::group(array('prefix' => 'profile-{id}'), function() {
        Route::get(CNamHoa::slugNH('system.my-profile'), [
            'as'   => 'profile',
            'uses' => 'ProfileController@index'
        ]);
        Route::get('image', [
            'as'   => 'profile-get-image',
            'uses' => 'ProfileController@getImage'
        ]);
        Route::group([
            'prefix' => 'form',
            'as'     => 'profile-form'
            ], function() {

            Route::get(null, 'ProfileController@getForm');
            Route::post(null, 'ProfileController@postForm');
        });
        Route::group([
            'prefix' => 'password',
            'as'     => 'profile-password'
            ], function() {

            Route::get(null, 'ProfileController@getPasswordForm');
            Route::post(null, 'ProfileController@postPasswordForm');
        });
        Route::group([
            'prefix' => 'image-form',
            'as'     => 'profile-image'
            ], function() {

            Route::get(null, 'ProfileController@getAjaxImage');
            Route::post(null, 'ProfileController@postAjaxImage');
        });
        Route::get('history/table', [
            'as'   => 'profile-table-history',
            'uses' => 'ProfileController@getAjaxTableHistory'
        ]);

        Route::group([
            'prefix' => 'brother',
            'as'     => 'profile-create-brother'
            ], function() {

            Route::get(null, 'ProfileController@getAjaxCreateBrother');
            Route::post(null, 'ProfileController@postAjaxCreateBrother');
        });
        Route::get('brother/table', [
            'as'   => 'profile-table-brother',
            'uses' => 'ProfileController@getAjaxTableBrother'
        ]);
        Route::post('brother/remove/{brotherId}', [
            'as'   => 'profile-remove-brother',
            'uses' => 'ProfileController@postAjaxRemoveBrother'
        ]);

        Route::group([
            'prefix' => 'dear',
            'as'     => 'profile-create-dear'
            ], function() {

            Route::get(null, 'ProfileController@getAjaxCreateDear');
            Route::post(null, 'ProfileController@postAjaxCreateDear');
        });
        Route::get('dear/table', [
            'as'   => 'profile-table-dear',
            'uses' => 'ProfileController@getAjaxTableDear'
        ]);
    });

    Route::group(array('prefix' => 'dear/{id}'), function() {
        Route::group([
            'prefix' => 'edit',
            'as'     => 'profile-edit-dear'
            ], function() {

            Route::get(null, 'ProfileController@getEditDear');
            Route::post(null, 'ProfileController@postAjaxEditDear');
        });
        Route::post('remove', [
            'as'   => 'profile-remove-dear',
            'uses' => 'ProfileController@postAjaxRemoveDear'
        ]);
    });

    Route::group(array('prefix' => CNamHoa::slugNH('navigation.manage-user-name')), function() {
        Route::group([
            'prefix' => CNamHoa::slugNH('navigation.manage-user.list'),
            'as'     => 'user-list'
            ], function() {

            Route::get(null, 'UserController@index');
            Route::post(null, 'UserController@postAjaxUserList');
        });

        Route::group(['prefix' => CNamHoa::slugNH('navigation.manage-user.create')], function() {
            Route::group([
                'as' => 'create-user'
                ], function() {

                Route::get(null, 'UserController@getCreation');
                Route::post(null, 'UserController@postCreation');
            });
            Route::post('file', [
                'as'   => 'create-user-by-file',
                'uses' => 'UserController@postAjaxCreationByFile',
            ]);
            Route::post('file/{cacheID}', [
                'as'   => 'create-user-by-file-table',
                'uses' => 'UserController@postAjaxCreationByFileTable',
            ]);
            Route::group([
                'prefix' => 'file/{cacheID}/{ID}',
                'as'     => 'create-user-by-file-edit'
                ], function() {

                Route::get(null, 'UserController@getAjaxCreationByFileEdit');
                Route::post(null, 'UserController@postAjaxCreationByFileEdit');
            });
        });
    });

    Route::group(array('prefix' => CNamHoa::slugNH('navigation.mornitor-name')), function() {
        Route::group(array('prefix' => CNamHoa::slugNH('navigation.mornitor.attendance')), function() {
            Route::get(null, 'AttendanceController@index');
            Route::post(null, [
                'as'   => 'attendance-user',
                'uses' => 'AttendanceController@postAjaxLoadUser'
            ]);
            Route::post('form', [
                'as'   => 'attendance-form',
                'uses' => 'AttendanceController@postAjaxLoadForm'
            ]);
            Route::post('update', [
                'as'   => 'attendance-update',
                'uses' => 'AttendanceController@postAjaxUpdate'
            ]);
        });

        Route::group(array('prefix' => CNamHoa::slugNH('navigation.mornitor.score')), function() {
            Route::get(null, 'ScoreController@index');
            Route::post(null, [
                'as'   => 'score-user',
                'uses' => 'ScoreController@postAjaxLoadUser'
            ]);
            Route::post('form', [
                'as'   => 'score-form',
                'uses' => 'ScoreController@postAjaxLoadForm'
            ]);
            Route::post('update', [
                'as'   => 'score-update',
                'uses' => 'ScoreController@postAjaxUpdate'
            ]);
        });

        Route::any(CNamHoa::slugNH('navigation.mornitor.report'), 'ReportController@index');
    });

    Route::group(array('prefix' => CNamHoa::slugNH('navigation.review')), function() {
        Route::get('{course?}', [
            'as'   => 'review-page',
            'uses' => 'ReviewController@index'
        ])->where('course', '[0-9]{4}');
        Route::post(null, [
            'as'   => 'review-load-user',
            'uses' => 'ReviewController@postLoadUser'
        ]);
        Route::post('form', [
            'as'   => 'review-form',
            'uses' => 'ReviewController@postAjaxForm'
        ]);
        Route::post('update', [
            'as'   => 'review-update',
            'uses' => 'ReviewController@postAjaxUpdate'
        ]);
    });

    Route::group(array('prefix' => CNamHoa::slugNH('navigation.classes')), function() {
        Route::group([
            'as' => 'class-list'
            ], function() {

            Route::get(null, 'ClassesController@index');
            Route::post(null, 'ClassesController@postAjaxClassList');
        });

        Route::group([
            'prefix' => 'create',
            'as'     => 'class-create'
            ], function() {
            Route::get(null, 'ClassesController@getAjaxCreateClass');
            Route::post(null, 'ClassesController@postAjaxCreateClass');
        });

        Route::group([
            'prefix' => 'edit/{id}',
            'as'     => 'class-edit'
            ], function() {
            Route::get(null, 'ClassesController@getAjaxUpdateClass');
            Route::post(null, 'ClassesController@postAjaxUpdateClass');
        });
        Route::get('detail/{id}', [
            'as'   => 'class-detail-user',
            'uses' => 'ClassesController@getAjaxDetailUser'
        ]);
        Route::post('remove', [
            'as'   => 'class-remove-user',
            'uses' => 'ClassesController@postAjaxRemoveUser'
        ]);
    });

    Route::get(CNamHoa::slugNH('navigation.document'), [
        'as'   => 'document',
        'uses' => 'DocumentController@index'
    ]);

    Route::group(array('prefix' => CNamHoa::slugNH('navigation.admin-name')), function() {
        Route::group(array('prefix' => CNamHoa::slugNH('navigation.admin.permission')), function() {
            Route::get(null, 'PermissionController@index');
            Route::post(null, [
                'as'   => 'permission-assign-list',
                'uses' => 'PermissionController@postAjaxAssignList'
            ]);
            Route::get('load/{page}', [
                'as'   => 'permission-load',
                'uses' => 'PermissionController@getAjaxLoadPage'
            ]);
            Route::group([
                'prefix' => 'create/{page}',
                'as'     => 'permission-create'
                ], function() {
                Route::get(null, 'PermissionController@getAjaxCreatePage');
                Route::post(null, 'PermissionController@postAjaxCreatePage');
            });
            Route::group([
                'prefix' => '{page}/edit/{id}',
                'as'     => 'permission-edit'
                ], function() {
                Route::get(null, 'PermissionController@getAjaxEditPage');
                Route::post(null, 'PermissionController@postAjaxEditPage');
            });
            Route::post('{page}/remove/{id}', [
                'as'   => 'permission-remove',
                'uses' => 'PermissionController@postAjaxRemovePage'
            ]);
        });

        Route::group(array('prefix' => CNamHoa::slugNH('navigation.admin.configuration')), function() {
            Route::get(null, 'OptionsController@index');
            Route::get('load/{page}', [
                'as'   => 'option-load',
                'uses' => 'OptionsController@getAjaxLoadPage'
            ]);
            Route::group([
                'prefix' => 'create/{page}',
                'as'     => 'option-create'
                ], function() {
                Route::get(null, 'OptionsController@getAjaxCreatePage');
                Route::post(null, 'OptionsController@postAjaxCreatePage');
            });
            Route::group([
                'prefix' => 'edit/{page}/{id}',
                'as'     => 'option-edit'
                ], function() {
                Route::get(null, 'OptionsController@getAjaxEditPage');
                Route::post(null, 'OptionsController@postAjaxEditPage');
            });
            Route::post('remove/{id}', [
                'as'   => 'option-remove',
                'uses' => 'OptionsController@postAjaxRemovePage'
            ]);
        });
    });
});
